package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.DateParseUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.AccountDetailDTO;
import top.zywork.dto.PagerDTO;
import top.zywork.enums.DatePatternEnum;
import top.zywork.query.AccountDetailQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.AccountDetailService;
import top.zywork.vo.AccountDetailVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ResponseStatusVO;

import java.math.BigDecimal;
import java.util.List;

/**
 * AccountDetailController控制器类<br/>
 *
 * 创建于2019-06-28<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/account-detail")
@Slf4j
public class AccountDetailController extends BaseController {

    private AccountDetailService accountDetailService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存AccountDetailVO")
    public ResponseStatusVO save(@RequestBody @Validated AccountDetailVO accountDetailVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        accountDetailService.save(BeanUtils.copy(accountDetailVO, AccountDetailDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存AccountDetailVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<AccountDetailVO> accountDetailVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        accountDetailService.saveBatch(BeanUtils.copyListObj(accountDetailVOList, AccountDetailDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除AccountDetailVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        accountDetailService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除AccountDetailVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        accountDetailService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新AccountDetailVO")
    public ResponseStatusVO update(@RequestBody @Validated AccountDetailVO accountDetailVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = accountDetailService.update(BeanUtils.copy(accountDetailVO, AccountDetailDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新AccountDetailVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<AccountDetailVO> accountDetailVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        accountDetailService.updateBatch(BeanUtils.copyListObj(accountDetailVOList, AccountDetailDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结AccountDetailVO")
    public ResponseStatusVO active(@RequestBody AccountDetailVO accountDetailVO) {
        accountDetailService.update(BeanUtils.copy(accountDetailVO, AccountDetailDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结AccountDetailVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<AccountDetailVO> accountDetailVOList) {
        accountDetailService.updateBatch(BeanUtils.copyListObj(accountDetailVOList, AccountDetailDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        AccountDetailVO accountDetailVO = new AccountDetailVO();
        Object obj = accountDetailService.getById(id);
        if (obj != null) {
            accountDetailVO = BeanUtils.copy(obj, AccountDetailVO.class);
        }
        return ResponseStatusVO.ok("查询成功", accountDetailVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = accountDetailService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), AccountDetailVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody AccountDetailQuery accountDetailQuery) {
        PagerDTO pagerDTO = accountDetailService.listAllByCondition(accountDetailQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), AccountDetailVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody AccountDetailQuery accountDetailQuery) {
        PagerDTO pagerDTO = accountDetailService.listPageByCondition(accountDetailQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), AccountDetailVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 用户分页查询账户明细
     * @param accountDetailQuery
     * @return
     */
    @PostMapping("user/pager-cond")
    public ResponseStatusVO userListPageByCondition(@RequestBody AccountDetailQuery accountDetailQuery) {
        accountDetailQuery.setUserId(SecurityUtils.getJwtUser().getUserId());
        return listPageByCondition(accountDetailQuery);
    }

    @GetMapping("admin/distribution-amount/{userId}")
    public ResponseStatusVO getDistributionAmount(@PathVariable("userId") Long userId) {
        BigDecimal amount = accountDetailService.sumGoodsOrderCommission(userId, null, null);
        return ResponseStatusVO.ok("查询成功", amount == null ? 0 : amount);
    }

    @PostMapping("admin/distribution-amount-by-time")
    public ResponseStatusVO getDistributionAmount(Long userId, String beginTime, String endTime) {
        BigDecimal amount = accountDetailService.sumGoodsOrderCommission(userId,
                org.apache.commons.lang3.StringUtils.isEmpty(beginTime) ? null : DateParseUtils.parseDate(beginTime, DatePatternEnum.DATETIME.getValue()),
                org.apache.commons.lang3.StringUtils.isEmpty(endTime) ? null : DateParseUtils.parseDate(endTime, DatePatternEnum.DATETIME.getValue()));
        return ResponseStatusVO.ok("查询成功", amount == null ? 0 : amount);
    }

    @GetMapping("user/distribution-amount")
    public ResponseStatusVO userGetDistributionAmount() {
        return getDistributionAmount(SecurityUtils.getJwtUser().getUserId());
    }

    @Autowired
    public void setAccountDetailService(AccountDetailService accountDetailService) {
        this.accountDetailService = accountDetailService;
    }
}
